<?php

class ProductsModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex() {
        $pager = new fvPager(ProductsManager::getInstance());
        $this->__assign('Products', $pager->paginate());
        return $this->__display('products_list.tpl');    
    }

    function showEdit() {
    	$request = fvRequest::getInstance();
        if (!$Products = ProductsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Products = new Products();
        }
        $pager = new fvPager(ProductsManager::getInstance());
        $editor = new fvFCKEditor("n[description]");
        $editor->Height = 360;
        $this->__assign(array(
            'Products' => $Products,
        	'editor' => $editor->CreateHtml($Products->description),
        ));
        return $this->__display('products_edit.tpl');
    }
    
    function showUploadimage() {
        if (fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename")) {
            $info = fvMediaLib::calcDementions(fvSite::$fvConfig->get('path.upload.tmp_dir') . fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename"),
                                                array('type' => Menu::IMAGE_TYPE_SMALL));
            echo fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename");
            return "<script>
                var fileName = '" . fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename")."';
                var width = '". $info[0] ."';
                var height = '". $info[1] ."';
                
                if (top.updateImage) {
                    top.updateImage(fileName, width, height);
                }
            </script>";
        } else {
            return "<script>
                alert('Произошла ошибка при загрузке файла. Проверьте файл и размер.');
            </script>";
        }
    }
    
}

?>
